import RoomListData from "../../model/item/RoomListData";

let app = getApp();
let globalStore = app.globalData.globalStore;

Page({
  data: {
    items:[]
  },
  onLoad: function() {
    // 绑定实时刷新回调
    globalStore.addRoomListener(this.onRoomUpdate.bind(this));
    // 使用初始化的数据更新列表
    this.onRoomUpdate(globalStore.roomList);
  },

  onRoomUpdate(rooms){
    const items = rooms.map((v)=>{
      const data =  new RoomListData();
      data.id = v.rid + '';
      data.avatar = v.avatar;
      data.name = v.name;
      data.body = '';
      data.unread = v.unread;
      data.owner = v.owner;
      data.lastMsgTime = v.lastMsgTime;
      data.announce = v.announce;
      data.introduce = v.introduce;

      return data;
    });

    this.setData({
      items : items
    });
  }
});
